package main

import (
	"context"
	"demo/message"
	"errors"
	"fmt"
	"github.com/micro/go-micro/v2"
)

/**
https://www.cnblogs.com/xiangxiaolin/p/12826710.html
go version: go1.15
go-micro微服务框架:
下载go-micro:
go get github.com/micro/go-micro [下载 v1.18.0]
go get github.com/micro/go-micro/v2 [下载 v2.9.1版本]
本项目用v2版本解决下面问题：
# github.com/micro/go-micro/transport/quic
D:\Go_get_library\pkg\mod\github.com\micro\go-micro@v1.18.0\transport\quic\quic.go:54:12: q.s.Close undefined (type quic.Session has no field or method Close)
 */

 //启动consul:consul agent -dev
 //服务注册到consul:go run server.go --registry=consul 注：go-micro v2弃用了consul作为默认的服务发现,默认使用mdns

 //狗血问题: github.com/micro/go-micro 默认版本，protoc-gen-micro工具不能用，而且此工具只能在github.com/micro/micro/v2版本，故go-micro使用go-micro/v2版本

type StudentManager struct {}

func (h *StudentManager) GetStudent(ctx context.Context, in *message.StudentRequest, out *message.Student) error {
	fmt.Println("接收到client端请求...")
	studentMap := map[string]message.Student{
		"jimmy":message.Student{Name:"jimmy",Classes:"二年一班",Grade:65},
		"ztind":message.Student{Name:"ztind",Classes:"二年二班",Grade:88},
		"jack":message.Student{Name:"jack",Classes:"二年三班",Grade:100},
	}
	student := studentMap[in.Name]
	if student.Name!=""{
		*out = student
		return nil
	}
	return errors.New(in.Name + "not exists")
}

func main(){
	//broker := mqtt.NewBroker() //a default to using env vars for master API
	//创建一个服务
	service := micro.NewService(
		micro.Name("student-server"),
		micro.Version("v1.0.0"),
		//micro.Broker(broker),
		)
	//初始化服务的option配置
	service.Init()
	//注册服务处理
	message.RegisterSetudenServiceHandler(service.Server(),&StudentManager{})
	//运行服务
	service.Run()
}
